//
//  HotViewController.m
//  谈诗说词
//
//  Created by dllo on 15/6/16.
//  Copyright (c) 2015年 lanou3g.com蓝鸥科技. All rights reserved.
//

#import "HotViewController.h"
#import "HeadView.h"
#import "ListCell.h"
#import "PomeDetailController.h"
#import "PoemDatabase.h"
#import "HotHeadView.h"
#import "PoemModel.h"

#define WIDTH [UIScreen mainScreen].bounds.size.width
#define HEIGHT [UIScreen mainScreen].bounds.size.height
@interface HotViewController ()<UITableViewDataSource, UITableViewDelegate>

@end

@implementation HotViewController
- (void)dealloc
{
    
}

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.array  = [NSMutableArray array];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view setBackgroundColor:[UIColor whiteColor]];
    self.navigationController.navigationBar.translucent = NO;
    UILabel *title = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 0.32 * WIDTH, 0.08 * WIDTH)];
    title.text = @"随机推荐";
    title.font = [UIFont fontWithName:@"-" size:28];
    self.navigationItem.titleView = title;
    UIImageView *back = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT - 64)];
    [back setImage:[UIImage imageNamed:@"左边背景.png"]];
    [self.view addSubview:back];
    // 右按钮
    UIImage * huan = [UIImage imageNamed:@"换.png"];
    // 图片原始颜色
    huan = [huan imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    UIBarButtonItem * right = [[UIBarButtonItem alloc] initWithImage:huan style:UIBarButtonItemStylePlain target:self action:@selector(huanAction:)];
    [self.navigationItem setRightBarButtonItem:right];
    
    
    self.array = [NSMutableArray arrayWithArray:[PoemDatabase selectAll]];
    [self createTable];
}
- (void)createTable
{
    self.table = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT - 64) style:UITableViewStylePlain];
    // table.rowHeight = 120;
    self.head = [[HotHeadView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, 0.54 * WIDTH)];
    
    self.number = arc4random()%400;
    PoemModel *model = [self.array objectAtIndex:self.number - 1];
    NSArray *arr = [model.content componentsSeparatedByString:@"。"];
    self.head.contentLabel.text = [NSString stringWithFormat:@"%@。", [arr firstObject]];
    self.head.titlabel.text = model.name;
    self.head.authorLabel.text = [NSString stringWithFormat:@"/%@/%@", model.dynasty_name, model.poet_name];
    self.head.target = self;
    self.head.action = @selector(topViewAction:);
    self.table.tableHeaderView = self.head;
    self.table.delegate = self;
    self.table.dataSource = self;
    [self.table setBackgroundColor:[UIColor clearColor]];
    self.table.alpha = 0.7;
    [self.view addSubview:self.table];

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 15;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ListCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (!cell) {
        cell = [[ListCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cell"];
        }
    [cell setBackgroundColor:[UIColor clearColor]];
    PoemModel *model = [self.array objectAtIndex:self.number + indexPath.row];
    cell.title.text = model.name;
    NSArray *array = [model.content componentsSeparatedByString:@"。"];
    NSString *content = [NSString stringWithFormat:@"%@。", [array firstObject]];
    NSDictionary *dic = @{NSFontAttributeName:[UIFont fontWithName:@"X-" size:19]};
    self.rect = [content boundingRectWithSize:CGSizeMake(WIDTH - 0.14 * WIDTH, 1000) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    cell.pome.frame = CGRectMake(0.07 * WIDTH, 0.22 * WIDTH, 0.86 * WIDTH, self.rect.size.height);
    cell.pome.text = content;
    
    cell.author.text = [NSString stringWithFormat:@"/%@/%@", model.dynasty_name, model.poet_name];
    
    return cell;

}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    PoemModel *model = [self.array objectAtIndex:indexPath.row + self.number];
    PomeDetailController *detail = [[PomeDetailController alloc]init];
    detail.model = model;
    detail.pomeTitle = model.name;
    detail.author = [NSString stringWithFormat:@"/%@/%@", model.dynasty_name, model.poet_name];
    detail.pome = model.content;
    CATransition *trans = [CATransition animation];
    [self.navigationController pushViewController:detail animated:NO];
    trans.type = @"rippleEffect";
    trans.duration = 0.8f;
    [self.navigationController.view.layer addAnimation:trans forKey:nil];
}
#pragma mark - cell自适应高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return self.rect.size.height + 0.26 * WIDTH;
}

- (void)topViewAction:(id)sender
{
    PoemModel *model = [self.array objectAtIndex:self.number-1];
    PomeDetailController *detail = [[PomeDetailController alloc]init];
    detail.model = model;
    detail.pomeTitle = model.name;
    detail.author = [NSString stringWithFormat:@"/%@/%@", model.dynasty_name, model.poet_name];
    detail.pome = model.content;
    CATransition *trans = [CATransition animation];
    [self.navigationController pushViewController:detail animated:NO];
    trans.type = @"rippleEffect";
    trans.duration = 0.8f;
    [self.navigationController.view.layer addAnimation:trans forKey:nil];

}

- (void)huanAction:(id)sender
{
    self.number = arc4random()%400;
    PoemModel *model = [self.array objectAtIndex:self.number - 1];
    NSArray *arr = [model.content componentsSeparatedByString:@"。"];
    self.head.contentLabel.text = [NSString stringWithFormat:@"%@。", [arr firstObject]];
    self.head.titlabel.text = model.name;
    self.head.authorLabel.text = [NSString stringWithFormat:@"/%@/%@", model.dynasty_name, model.poet_name];
    [self.table reloadData];
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
